import java.io.*;
import java.util.*;
import local.util.MyPrint;

/*******************************************************************
 * public void setTickName (String tick)
 * public String getGroupIdByTick ()
 * public String getGroupNameById (String ID)
 *
 *******************************************************************/

public class FindGroupName {
    
    private String tickName = null;

    public static void main(String[] args) {

	if (args.length<1) {
	    System.out.println("============================================");
	    System.out.println("Input: Tick Name; Output: Group Name and Id");
	    System.out.println("============================================");
	    System.exit(0);
	}

	String tick = args[0];
	FindGroupName gf = new FindGroupName ();
	Hashtable gid_gname_hashtable = new Hashtable ();
	gid_gname_hashtable = gf.getGroupNameByTick(tick);
	MyPrint.printLine("=",40);
	System.out.println("Found:\t"+tick+" In:");
	MyPrint.printLine("-",40);
	MyPrint.printHashTable(gid_gname_hashtable);
	MyPrint.printLine("=",40);
	System.out.println(gid_gname_hashtable.toString());
    }

    public void setTickName(String tick) {
	tickName = tick;
	System.out.println("FindGroupName:setTick("+tick+")");
    }

    public String getGroupNameById (String id) {

	String group_name = null;
	String key = null;
	Hashtable prof_hashtable = new Hashtable();

	GroupProfileLoader gid_loader = new GroupProfileLoader();

	if (id==null) {
	    group_name = "Unknow";
	}
	else {	
	    gid_loader.setFileName("./Data/Group/IndustryGroup.txt");
	    gid_loader.loadProfile();
	    prof_hashtable = gid_loader.getHashTable();

	    Enumeration enum = prof_hashtable.keys();  	   
	    while (enum.hasMoreElements()) {
		key = (String)enum.nextElement();
	   
		if (id.equals(key.trim())) {
		    group_name= (String)prof_hashtable.get(key);
		    break;
		}
	    }
	}

	return group_name;
    }

    public String getGroupIdByTick () {

	String tick     = tickName;
	String g_id     = null;
	String group_id = null;

	File file;

	Hashtable tick_hashtable = new Hashtable();
	Hashtable g_p_hashtable = new Hashtable();

	GroupProfileLoader gpfloader = new GroupProfileLoader();
	gpfloader.setFileName("C:/myProjects/stock/Data/Group/IndustryGroup.txt");
	gpfloader.loadProfile();
	g_p_hashtable = gpfloader.getHashTable();

	TickFileLoader floader = new TickFileLoader ();

	Enumeration enum1 = g_p_hashtable.keys();

	while (enum1.hasMoreElements()) {
	    g_id = (String)enum1.nextElement();

	    file = new File ("./Data/Group/"+g_id+".txt");

	    if (file.isFile()) {

		floader.setFileName(file);
		floader.loadFile();

		tick_hashtable = floader.getHashTable();
	
		if (tick_hashtable.contains(tick)) {
		    group_id = g_id;
		    break;
		    //System.out.print("Found  :"+tick+ "  in "+ g_id+".txt   ");
		    //System.out.println("\t"+g_p_hashtable.get(g_id));
		}
	    }
	}

	if (group_id == null) {
	    group_id = "130";
	    //System.out.println("Group   Id:   "+group_id);
	    //System.out.println("Group Name:  Unknow");
	}

	return group_id;
    }

    public Hashtable getGroupNameByTick (String tick) {

	String group_id     = null;
	String group_name   = null;

	File file;

	Hashtable tick_hashtable = new Hashtable();
	Hashtable g_p_hashtable = new Hashtable();
	Hashtable return_hashtable = new Hashtable ();

	GroupProfileLoader gpfloader = new GroupProfileLoader();
	gpfloader.setFileName("./Data/Group/IndustryGroup.txt");
	gpfloader.loadProfile();
	g_p_hashtable = gpfloader.getHashTable();

	TickFileLoader floader = new TickFileLoader ();

	Enumeration enum = g_p_hashtable.keys();

	while (enum.hasMoreElements()) {
	    group_id = (String)enum.nextElement();

	    file = new File ("./Data/Group/"+group_id+".txt");

	    if (file.isFile()) {

		floader.setFileName(file);
		floader.loadFile();

		tick_hashtable = floader.getHashTable();
	
		if (tick_hashtable.contains(tick)) {
		    group_name = (String)g_p_hashtable.get(group_id);
		    return_hashtable.put(group_id, group_name);
		}
	    }
	}

	if (group_name == null) {
	    return_hashtable.put("130","Unknow");
	    group_name = "Unknow";
	}

	return return_hashtable;
    }
}






